package com.edu.gcu.system.util;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author polo
 * @version V1.0
 * @description: ftp工具类
 * @date 2018-02-10-16:48
 */

public class FTPUtil {

    private static Logger logger = LoggerFactory.getLogger(FTPUtil.class);

    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");

    public FTPUtil(String ip, int port, String user, String pwd) {
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pwd = pwd;
    }

    // 批量上传
    public static boolean uploadFile(List<File> fileList) throws IOException {
        FTPUtil ftpUtil = new FTPUtil(ftpIp, 21, ftpUser, ftpPass);
        logger.info("开始连接ftp服务器");
        boolean result = ftpUtil.uploadFile("img", fileList);
        logger.info("开始连接ftp服务器,图片上传完毕");
        return result;
    }

    // 删除
    public static boolean deleteFile(String file) throws IOException {
        FTPUtil ftpUtil = new FTPUtil(ftpIp, 21, ftpUser, ftpPass);
        logger.info("开始连接ftp服务器");
        boolean result = ftpUtil.removeFile("img", file);
        logger.info("开始连接ftp服务器,图片删除完毕");
        return result;
    }

    public boolean removeFile(String remotePath, String file) throws IOException {
        boolean uploaded = true;
        FileInputStream fis = null;

        //连接ftp
        if (connectServer(this.ip, this.port, this.user, this.pwd)) {
            try {
                // 更改工作目录 是否需要切换文件夹 为空则不切
                ftpClient.changeWorkingDirectory(remotePath);
                // 设置编码格式
                ftpClient.setControlEncoding("UTF-8");
                // 设置缓冲区
                ftpClient.setBufferSize(1024);
                // 设置文件类型为二进制文件
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                // 打开被动模式
                ftpClient.enterLocalPassiveMode();
                // ftp客户端删除文件
                ftpClient.dele(file);
            } catch (IOException e) {
                logger.error("删除文件异常", e);
                uploaded = false;
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    fis.close();
                }
                ftpClient.disconnect();
            }
        }
        return uploaded;
    }

    private boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
        boolean uploaded = true;
        FileInputStream fis = null;

        //连接ftp
        if (connectServer(this.ip, this.port, this.user, this.pwd)) {
            try {
                // 更改工作目录 是否需要切换文件夹 为空则不切
                ftpClient.changeWorkingDirectory(remotePath);
                // 设置缓冲区
                ftpClient.setBufferSize(1024);
                // 设置编码格式
                ftpClient.setControlEncoding("UTF-8");
                // 设置文件类型为二进制文件
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                // 打开被动模式
                ftpClient.enterLocalPassiveMode();
                //存储文件
                for (File file : fileList) {
                    // 文件读取到输入流
                    fis = new FileInputStream(file);
                    // ftp客户端存储文件
                    ftpClient.storeFile(file.getName(), fis);
                }
            } catch (IOException e) {
                logger.error("上传文件异常", e);
                uploaded = false;
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    fis.close();
                }
                ftpClient.disconnect();
            }
        }
        return uploaded;
    }

    // 连接FTP
    private boolean connectServer(String ip, int port, String user, String pwd) {
        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user, pwd);
        } catch (IOException e) {
            logger.error("连接ftp出错", e);
        }
        return isSuccess;
    }

    private String ip;
    private int port;
    private String user;
    private String pwd;
    private FTPClient ftpClient;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}
